package com.qf.driver.service.impl;

import com.qf.common.bean.R;
import com.qf.common.constant.CommonConstant;
import com.qf.common.dto.CmdParamDto;
import com.qf.common.dto.DeviceDto;
import com.qf.common.dto.DeviceTopicDto;
import com.qf.common.model.DeviceCmd;
import com.qf.common.model.QfDevice;
import com.qf.common.model.QfDeviceTopic;
import com.qf.common.model.QfProduct;
import com.qf.common.utils.JsonUtil;
import com.qf.driver.feign.DeviceFeign;
import com.qf.driver.feign.DeviceTopicFeign;
import com.qf.driver.feign.ProductFeign;
import com.qf.driver.mqtt.bean.MqttProperties;
import com.qf.driver.mqtt.handler.MqttSendHandler;
import com.qf.driver.service.CmdService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author 千锋健哥
 */
@Service
public class CmdServiceImpl implements CmdService {

    @Autowired
    private DeviceTopicFeign deviceTopicFeign;

    @Autowired
    private MqttSendHandler mqttSendHandler;

    @Autowired
    private MqttProperties mqttProperties;

    @Autowired
    private DeviceFeign deviceFeign;

    @Autowired
    private ProductFeign productFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public R send(CmdParamDto dto) {
        //1. 根据设备key, 查询对应的topic列表, 因为每个设备有一对topic
        DeviceTopicDto deviceTopicDto = new DeviceTopicDto();
        deviceTopicDto.setDeviceKey(dto.getDeviceKey());
        R<List> topicResult = deviceTopicFeign.findAll(deviceTopicDto);
        if (topicResult == null) {
            return R.fail("该设备key找不到对应的topic主题!");
        }
        String jsonStr = JsonUtil.toJsonString(topicResult.getData());
        List<QfDeviceTopic> topicList = JsonUtil.parseArray(jsonStr, QfDeviceTopic.class);


        //2. 组装指令, 组装成json格式
        String cmdJson = JsonUtil.toJsonString(dto);

        //3. 从topic列表中找出对应的发送指令的topic
        String topic = null;

        if (topicList != null) {
            for (QfDeviceTopic qfDeviceTopic : topicList) {
                if (qfDeviceTopic.getTopic().contains("/qfjava/device/ctrl")) {
                    topic = qfDeviceTopic.getTopic();
                }
            }
        }

        //4. 发送指令
        try {
            mqttSendHandler.sendMqtt(topic, mqttProperties.getQos(), cmdJson);
        } catch (Exception e) {
            mqttSendHandler.sendMqtt(topic, cmdJson);
        }

        //5. 封装指令日志对象, 将对象发送到rabbitmq中, 由data微服务将指令日志保存到mongodb
        DeviceDto deviceDto = new DeviceDto();
        deviceDto.setDeviceKey(dto.getDeviceKey());
        R<List> deviceResult = deviceFeign.findAreaAll(deviceDto);
        if (deviceResult != null) {
            List deviceList = deviceResult.getData();
            if (deviceList != null && deviceList.size() > 0){
                String deviceJson = JsonUtil.toJsonString(deviceList.get(0));
                QfDevice qfDevice = JsonUtil.parseObject(deviceJson, QfDevice.class);

                //封装设备指令对象
                DeviceCmd deviceCmd = new DeviceCmd();
                deviceCmd.setCreateTime(new Date());
                deviceCmd.setTenantId(qfDevice.getTenantId());
                deviceCmd.setDeviceKey(dto.getDeviceKey());
                deviceCmd.setCmdName(dto.getCmdName());
                deviceCmd.setValue(dto.getValue());
                deviceCmd.setDeviceSubCode(dto.getDeviceSubCode());

                R<QfProduct> productResult = productFeign.selectById(String.valueOf(qfDevice.getProductId()));
                QfProduct product = productResult.getData();
                deviceCmd.setProductKey(product.getProductKey());

                rabbitTemplate.convertAndSend(CommonConstant.Rabbit.TOPIC_EXCHANGE_VALUE,
                        CommonConstant.Rabbit.ROUTING_DEVICE_CMD_PREFIX + "cmd",
                        deviceCmd);
            }
        }




        return null;
    }

}
